package Demo01;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—06—22
 * Time: 16:07
 */
class Food {
}

class Fruit extends Food {
}

class Apple extends Fruit {
}

class Banana extends Fruit {
}

class Test<T> { // 设置泛型上限
    private T val ;

    public T getVal() {
        return val;
    }

    public void setVal(T Val) {
        this.val = val;
    }
}
public class Test03 {
    public static void main(String[] args) {
        Test<Fruit> test = new Test<>();
        test.setVal(new Fruit());
        fun2(test);

        Test<Food> test2 = new Test<>();
        test2.setVal(new Food());
        fun2(test2);
    }
    public static void fun2(Test<? super Fruit> temp){
      // 此时可以修改！！添加的是Fruit 或者Fruit的子类
        temp.setVal(new Apple());//这个是Fruit的子类
        temp.setVal(new Fruit());//这个是Fruit的本身
        // Fruit fruit = temp.getMessage(); 不能接收，这里无法确定是哪个父类
        System.out.println(temp.getVal());//只能直接输出
    }

    public static void main1(String[] args) {
        Test<Apple> test = new Test<>() ;
        test.setVal(new Apple());
        fun(test);

        Test<Banana> test2 = new Test<>() ;
        test2.setVal(new Banana());
        fun(test2);
    }
    //只要是Fruit或者Fruit的子类即可
    public static void fun(Test<? extends Fruit> temp){
//        temp.setVal(new Banana());
//        temp.setVal(new Apple());
        //放的都是Fruit或者Fruit的子类，所以可以直接使用Fruit来接收
        Fruit banana = temp.getVal();
         System.out.println(temp.getVal());
    }
}
